package com.zhuangfei.hos_timetableview.ability;

import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.hos_timetableview.utils.ToastUtil;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.model.Schedule;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

import java.util.List;

/**
 * 使用自定义属性对TimetableView进行配置<br/>
 * xml中只能配置部分属性，知道有这个东西即可，
 * 建议使用代码进行配置更加灵活
 */
public class AttrAbility extends Ability {

    private static final String TAG = "AttrActivity";

    TimetableView mTimetableView;
    List<MySubject> mySubjects;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_attr);
        mySubjects = SubjectRepertory.loadDefaultSubjects();
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        // 获取控件
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        mTimetableView.source(mySubjects)
                .callback(new ISchedule.OnItemClickListener() {
                    @Override
                    public void onItemClick(Component component, List<Schedule> scheduleList) {
                        display(scheduleList);
                    }
                })
                .callback(new ISchedule.OnWeekChangedListener() {
                    @Override
                    public void onWeekChanged(int curWeek) {
                        ToastUtil.showToast(AttrAbility.this, "第" + curWeek + "周");
                    }
                })
                .showView();
    }

    /**
     * 显示内容
     *
     * @param beans 数据源
     */
    protected void display(List<Schedule> beans) {
        String str = "";
        for (Schedule bean : beans) {
            str += bean.getName() + "、";
        }
        ToastUtil.showToast(this, str);
    }
}
